package util;

import common.FileRedefined;

import java.io.*;

public class StreamUtils {
    public static byte[] streamToByteArray(InputStream is){
        try {
            ByteArrayOutputStream bos=new ByteArrayOutputStream();
            byte[] buf=new byte[1024];
            int len=0;
            //读取一个字节的数据，读到末尾返回-1
            while ((len=is.read(buf))!=-1){
                bos.write(buf,0,len);
            }
            byte[] array=bos.toByteArray();
            bos.close();
            is.close();
            return array;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件读取失败");
        }
        return null;
    }
    public static void byteArrayToStream(byte[] array,String path){
        FileOutputStream fileOutputStream= null;
        try {
            fileOutputStream = new FileOutputStream(path);
            fileOutputStream.write(array);
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("文件下载失败");
        }
    }
}
